/*
 * Copyright © 2001-2010 Stéphane Raimbault <stephane.raimbault@gmail.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#ifndef _TCP_H
#define _TCP_H

#include <signal.h>
#include <sys/types.h>
# include <sys/socket.h>
# include <sys/ioctl.h>
# include <netinet/in.h>
# include <netinet/ip.h>
# include <netinet/tcp.h>
# include <arpa/inet.h>
# include <netdb.h>
#include <sys/select.h>

#define MAX_TCP_LEN 1400
#define TCP_RESPONSE_TIMEOUT    500000
#define TCP_BYTE_TIMEOUT        500000

typedef struct
{
    int debug;
    int s;
    /* TCP port */
    int port;
    /* IP address */
    char ip[16];

    struct timeval response_timeout;
    // struct timeval byte_timeout;
    // struct timeval indication_timeout;
} tcp_t;


extern int tcp_connect(tcp_t *ctx);
extern void tcp_close(tcp_t *ctx);
extern int tcp_flush(tcp_t *ctx);
extern int tcp_listen(tcp_t *ctx, int nb_connection);
extern int tcp_accept(tcp_t *ctx, int *s);
extern int tcp_select(tcp_t *ctx, int timeout);
extern void tcp_free(tcp_t *ctx);
extern tcp_t* new_tcp(const char *ip, int port);
extern ssize_t tcp_send(tcp_t *ctx, const uint8_t *req, int req_length);
extern ssize_t tcp_recv(tcp_t *ctx, uint8_t *rsp, int rsp_length,int timeout);

#endif /* MODBUS_TCP_H */
